# Author: Mark Richardson
# Purpose: Marge latent policy disagreement with agency ideology estimates, agency skills
#          estimates, and mean perceptions of agenda change

# Load packages
library(dplyr)
library(stringr)

# Load data

load("data/01_dis_data.RData")

ideo <- readr::read_csv("data/rcl_ideology_estimates.csv")

skills <- readr::read_csv("data/rcl_skills_estimates.csv")

#### Merge ideology estimates

# Identify non-matches
nm <- anti_join(dis_data, ideo,
                by = "agency")

# Format dept variable and adjust agency names
ideo <- ideo %>%
  mutate(agency = case_when(agency == "Department of the Army" ~ "Army",
                            agency == "Department of the Navy" ~ "Navy",
                            agency == "Department of the Air Force" ~ "Air Force",
                            agency == "Arms Control and International Security" ~ "Arms Control and International Security Affairs",
                            agency == "Federal Housing Administration" ~ "Federal Housing Administration/Office of Housing",
                            TRUE ~ agency),
         agency = case_when(agency == "Department of Commerce" ~ "Office of the Secretary of Commerce",
                            agency == "Department of Health and Human Services" ~ "Office of the Secretary of Health and Human Services",
                            agency == "Department of State" ~ "Office of the Secretary of State",
                            agency == "Department of Transportation" ~ "Office of the Secretary of Transportation",
                            agency == "Department of the Interior" ~ "Office of the Secretary of the Interior",
                            TRUE ~ agency)) 

# Check non-matches after adjustments
nm_post_ideo <- anti_join(dis_data, ideo,
                          by = "agency") # No ideo estimates for these agencies

# Execute the merge
dis_data_wt <- left_join(dis_data, ideo %>% select(!c(dept, acr)),
                         by = "agency")

#### Merge skills estimates ####

# Identify non-matches
nm <- anti_join(dis_data, skills,
                by = "agency")

# Format dept variable and adjust agency names
skills <- skills %>%
  mutate(agency = case_when(agency == "Department of the Army" ~ "Army",
                            agency == "Department of the Navy" ~ "Navy",
                            agency == "Department of the Air Force" ~ "Air Force",
                            agency == "Arms Control and International Security" ~ "Arms Control and International Security Affairs",
                            agency == "Federal Housing Administration" ~ "Federal Housing Administration/Office of Housing",
                            TRUE ~ agency),
         agency = case_when(agency == "Department of Commerce" ~ "Office of the Secretary of Commerce",
                            agency == "Department of Health and Human Services" ~ "Office of the Secretary of Health and Human Services",
                            agency == "Department of State" ~ "Office of the Secretary of State",
                            agency == "Department of Transportation" ~ "Office of the Secretary of Transportation",
                            agency == "Department of the Interior" ~ "Office of the Secretary of the Interior",
                            TRUE ~ agency))

# Check non-matches after adjustments
nm_post_skills <- anti_join(dis_data, skills,
                            by = "agency") # No skills estimates for these agencies

# Execute the merge
dis_data_wt <- left_join(dis_data_wt, skills %>% select(!c(dept, acr)),
                         by = "agency")

#### Create dept_acr variable ####

dis_data_wt <- dis_data_wt %>%
  mutate(dept_acr = case_when(dept == "Department of Agriculture" ~ "USDA",
                              dept == "Department of Commerce" ~ "COM",
                              dept == "Department of Defense" ~ "DOD",
                              dept == "Department of Education" ~ "ED",
                              dept == "Department of Energy" ~ "DOE",
                              dept == "Department of Health and Human Services" ~ "HHS",
                              dept == "Department of Homeland Security" ~ "DHS",
                              dept == "Department of Housing and Urban Development" ~ "HUD",
                              dept == "Department of Justice" ~ "DOJ",
                              dept == "Department of Labor" ~ "DOL",
                              dept == "Department of State" ~ "STAT",
                              dept == "Department of the Interior" ~ "DOI",
                              dept == "Department of the Treasury" ~ "TREAS",
                              dept == "Department of Transportation" ~ "DOT",
                              dept == "Department of Veterans Affairs" ~ "VA"))

#### Save the results ####

save(dis_data_wt, file = "data/05_dis_data_w_traits.RData")
      
readr::write_csv(dis_data_wt, file = "data/05_dis_data_w_traits.csv")
